<?php

class TransportationPlugin {
    var $ld;
	var $settings;
	
	var $BusStopMainObjects = array();
	var $SubwayMainObjects = array();
	var $SubStopMainObjects = array();
	var $BusStopDTObjects = array();
	var $SubwayDTObjects = array();
	var $SubStopDTObjects = array();

	
	function TransportationPlugin()
	{
		$this->ld = new LocalData();
	}
	
	function GetForm()
	{
		//global $imagePath;
		//$imagePath = substr($_SERVER["PHP_SELF"], 0, strpos($_SERVER["PHP_SELF"], "/"));
		$PlVersion = (array)$this->ld->GetPlVersion('Transportation');
		$this->settings = (array) $this->ld->GetPlSettings('Transportation');
	  
		echo '	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
				"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
				<html xmlns="http://www.w3.org/1999/xhtml" lang="ru">
					<head>
						<title>Subway</title>
						<script src="..\..\codebase-php\jquery-1.4.2.min.js"></script>
						<script src="..\..\codebase-php\jquery.json-2.2.min.js"></script>
						<link href="helpers/plugin.css" rel="stylesheet" type="text/css">
						<style>
							#maint{ font-size: 10pt; width: 850px; padding: 0px; }
						</style>
						
						<script language="JavaScript" type="text/javascript">
							<!--
								var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
								
								function addOption(theSel, theText, theValue)
								{
									var newOpt = new Option(theText, theValue);
									var selLength = theSel.length;
									theSel.options[selLength] = newOpt;
								}

								function deleteOption(theSel, theIndex)
								{
									var selLength = theSel.length;
									if(selLength>0)
									{
										theSel.options[theIndex] = null;
									}
								}

								function moveOptions(theSelFrom, theSelTo)
								{
									var selLength = theSelFrom.length;
									var selectedText = new Array();
									var selectedValues = new Array();
									var selectedCount = 0;
									var i;
									// Find the selected Options in reverse order and delete them from the from Select.
									for(i=selLength-1; i>=0; i--)
									{
										if(theSelFrom.options[i].selected)
										{
											selectedText[selectedCount] = theSelFrom.options[i].text;
											selectedValues[selectedCount] = theSelFrom.options[i].value;
											deleteOption(theSelFrom, i);
											selectedCount++;
										}
									}

									// Add the selected text/values in reverse order.
									// This will add the Options to the TO Select
									// in the same order as they were in the FROM Select.
									for(i=selectedCount-1; i>=0; i--)
									{
										addOption(theSelTo, selectedText[i], selectedValues[i]);
									}

									if(NS4) history.go(0);
								}
							//-->
						</script>


						<script language="javascript">
							<!-- For select images box
								function showimage()
								{
									if (!document.images) return
									document.images.pictures.src=document.mygallery.picture.options[document.mygallery.picture.selectedIndex].value
								}
							//-->
						</script>

						<script>
						function sortSelect(selElem) {
							var tmpAry = new Array();
							for (var i=0;i<selElem.options.length;i++) {
								tmpAry[i] = new Array();
								tmpAry[i][0] = selElem.options[i].text;
								tmpAry[i][1] = selElem.options[i].value;
							}
							tmpAry.sort();
							while (selElem.options.length > 0) {
								selElem.options[0] = null;
							}
							for (var i=0;i<tmpAry.length;i++) {
								var op = new Option(tmpAry[i][0], tmpAry[i][1]);
								selElem.options[i] = op;
							}
							return;
						}

						window.reload = function(){
							var l=window.location.toString();
							var indx=l.indexOf(\'?\');
							window.location=l.slice(0, indx)+"?action=refresh&tmp="+Math.random();
						}
						//==============================================================
						$(document).ready(function(){
							window.settings=eval(' . json_encode($this->ld->GetPlSettings('Transportation')) . ');
							if ((window.settings!==null) && (window.settings!==undefined))
							{
								$(":checkbox").each(function(){
									if(window.settings[$(this).attr("id")]==true){
										$(this).attr("checked", true);
									}else{
										$(this).attr("checked", false);
									}
								});

								$(":radio").each(function(){
									if(window.settings[$(this).attr("id")]==true){
										$(this).attr("checked", true);
									}else{
										$(this).attr("checked", false);
									}
								});
									
								$(":text").each(function (){
									var attr=$(this).attr("id");
									if (window.settings[attr]!==null){
										$(this).val(window.settings[attr]);
									}else{
										$(this).val("0");
									}
								});
							}

							
							//==============================================================
							$("#btn_save").click(function(){
								var req=new Object();
								$(":checkbox").each(function(){
									var par=$(this).attr("id");
									req[par]=$(this).attr("checked");
									if (($(this).attr("objid")!==null) && ($(this).attr("objid")!==undefined))
									{
										req[par+"ID"]=$(this).attr("objid");
									}
								});
								$(":radio").each(function(){
									var par=$(this).attr("id");
									req[par]=$(this).attr("checked");
									if (($(this).attr("objid")!==null) && ($(this).attr("objid")!==undefined))
									{
										req[par+"ID"]=$(this).attr("objid");
									}
								});
								$("input:text").each(function(){
									req[$(this).attr("id")]=$(this).val();
								});
										
								req.collectSubwayMainIN=new Array();
								$("#collectSubwayMainIN option").each(function(){
									var par=$(this).val();
									req.collectSubwayMainIN.push(par);
								});
										
								req.collectBusMainIN=new Array();
								$("#collectBusMainIN option").each(function(){
									var par=$(this).val();
									req.collectBusMainIN.push(par);
								});

								req.collectSubwayDTIN=new Array();
								$("#collectSubwayDTIN option").each(function(){
									var par=$(this).val();
									req.collectSubwayDTIN.push(par);
								});
										
								req.collectBusDTIN=new Array();
								$("#collectBusDTIN option").each(function(){
									var par=$(this).val();
									req.collectBusDTIN.push(par);
								});

								data=$.toJSON(req);
								var l=window.location.toString();
								var indx=l.indexOf(\'?\');
								var nurl=l.slice(0, indx)+"?action=save&tmp="+Math.random();
								$.post(nurl, data);

								return false;
							});
						});
						</script>
					</head>
					
					<body>
						<form name="mygallery">
							<table border="1" cellspacing="1" cellpadding="1" width="100%" bgcolor="#DAA520">
								<tr height="23">
									<td><hr><h1>Transportation&nbsp;v'.$PlVersion['version'].'</h1>
									<h5>Written by Little Todd</h5><hr></td>
								</tr>
							</table>
							<br>
							<table border="1" cellspacing="1" cellpadding="1" width="100%" bgcolor="#ff0066">
								<tr height="23">
									<td><hr><h3><input type="checkbox" id="echeck">Auto-Check/Use Energy from your inventory?</h3><hr></td>
								</tr>
							</table>			
							<br>';
							
		$this->ClassifyObjects();
							
		if (count($this->BusStopMainObjects) || count($this->BusStopDTObjects))
		{

			echo 			$this->OutputHeader("Bus");
			echo '			<input type="checkbox" id="actionBus">Send Commuters then send Bus to each Bus Stop<br>
								<tr height="12">
									<td><input type="checkbox" id="rBusCollect">REPEAT</td>
									<td><input type="text" id="rBusTimes" value="0" size="4">times</td>
									<br>
								</tr>
								<br>';
			echo 			$this->BusMainOutput();
			echo 			$this->BusDTOutput();
		}
							
		if (count($this->SubwayMainObjects) || count($this->SubwayDTObjects) || count($this->SubStopMainObjects) || count($this->SubStopDTObjects))
		{

			echo 			$this->OutputHeader("Subway");
			echo '			<input type="checkbox" id="actionSubway">Supply/Visit/Collect Subway Entrances, then Collect Subway Stop<br>
							<h5>Note: Subway Stop will be collected from first if collectable.</h5>
								<tr height="12">
									<td><input type="checkbox" id="rSubCollect">REPEAT</td>
									<td><input type="text" id="rSubTimes" value="0" size="4">times</td>
									<br>
								</tr>
								<br>
							<input type="checkbox" id="supplyAfter">Supply Subway Entrances After Collecting Stop<br>
							<br>';
			echo 			$this->SubwayMainOutput();
			echo 			$this->SubwayDTOutput();
			echo 			$this->SubStopOutput();
		}
							
echo '						<div width="100%" align="center"><hr>
								<button id="btn_save" style="color:white;background-color:#13a89e;border-width:1px;border-style:solid; ">&nbsp;Save Settings&nbsp;</button>
							</div>
							<hr>
						</form>
					</body>
				</html>';
	} // End GetForm()

	function OutputHeader($title)
	{
		echo '				<table border="1" cellspacing="1" cellpadding="1" width="100%" bgcolor="#ff0066">
								<tr height="23"><td><h3>'.$title.'</h3></td></tr>
							</table>
							<br>';
	}

	
	function ClassifyObjects()
	{
		$objects = $this->ld->GetObjects();
		
		foreach ($objects as $obj)
		{
			switch ($obj['className'])
			{
				case 'BusStop';
					$this->BusStopMainObjects[] = $obj;
					break;
				case 'Subway';
					$this->SubwayMainObjects[] = $obj;
					break;
				case 'SubwayReceiver';
					$this->SubStopMainObjects[] = $obj;
					break;
			}
		}

		$objects = $this->ld->GetDTObjects();
		foreach ($objects as $obj)
		{
			switch ($obj['className'])
			{
				case 'BusStop';
					$this->BusStopDTObjects[] = $obj;
					break;
				case 'Subway';
					$this->SubwayDTObjects[] = $obj;
					break;
				case 'SubwayReceiver';
					$this->SubStopDTObjects[] = $obj;
					break;
			}
		}
	}

	function BusMainOutput()
	{
		$display = false;
		$output = 'Select the Main World Bus Stops you like to process:';
		if(!isset($this->settings['collectBusMainIN'])) $this->settings['collectBusMainIN'] = array();
		$resNOT="";
		$resN = array();
		$resIN="";
		$resI = array();
		foreach ($this->BusStopMainObjects as $key=>$item)
		{
			$display = true;
			$found = "N";
			if (in_array((string)$item['id'],$this->settings['collectBusMainIN'])) {$found = "Y";}
			if($found == "Y" )
				{ $resI[$item['id']] = (string)$item['itemName'];}
			else
				{ $resN[$item['id']] = (string)$item['itemName'];}
		}
		natcasesort($resI);
		natcasesort($resN);
		foreach ($resI as $id=>$item)
		{
			$resIN .= '<option value="'.$id.'">'.$item." ($id)</option>";
		}
		foreach ($resN as $id=>$item)
		{
			$resNOT .= '<option value="'.$id.'">'.$item." ($id)</option>";
		}
		$output .= '<table class="table0">
						<th>Do NOT collect</th><th></th><th>Do collect</th>
						<tr>
							<td>
								<select name="BusMain1" size="10" multiple="multiple">';
		$output .=					$resNOT;
		$output .= '			</select>
							</td>
							<td align="center" valign="middle">
								<input type="button" value="--&gt;"onclick="moveOptions(this.form.BusMain1, this.form.BusMain2); sortSelect(this.form.BusMain2)" /><br />
								<input type="button" value="&lt;--"onclick="moveOptions(this.form.BusMain2, this.form.BusMain1); sortSelect(this.form.BusMain1)" />
							</td>
							<td>
								<select name="BusMain2" size="10" multiple="multiple" id="collectBusMainIN">';
		$output .= 					$resIN;
		$output .= '   			</select>
							</td>
						</tr>
					</table>';
		$output .= '<br>';
		if ($display)
			return $output;
		else
			return "";
	}
	
	function BusDTOutput()
	{
		$display = false;
		$output = 'Select the Downtown Bus Stops you like to process:';
		if(!isset($this->settings['collectBusDTIN'])) $this->settings['collectBusDTIN'] = array();
		$resNOT="";
		$resN = array();
		$resIN="";
		$resI = array();
		foreach ($this->BusStopDTObjects as $key=>$item)
		{
			$display = true;
			$found = "N";
			if (in_array((string)$item['id'],$this->settings['collectBusDTIN'])) {$found = "Y";}
			if($found == "Y" )
				{ $resI[$item['id']] = (string)$item['itemName'];}
			else
				{ $resN[$item['id']] = (string)$item['itemName'];}
		}
		natcasesort($resI);
		natcasesort($resN);
		foreach ($resI as $id=>$item)
		{
			$resIN .= '<option value="'.$id.'">'.$item." ($id)</option>";
		}
		foreach ($resN as $id=>$item)
		{
			$resNOT .= '<option value="'.$id.'">'.$item." ($id)</option>";
		}
		$output .= '<table class="table0">
						<th>Do NOT collect</th><th></th><th>Do collect</th>
						<tr>
							<td>
								<select name="BusDT1" size="10" multiple="multiple">';
		$output .=					$resNOT;
		$output .= '			</select>
							</td>
							<td align="center" valign="middle">
								<input type="button" value="--&gt;"onclick="moveOptions(this.form.BusDT1, this.form.BusDT2); sortSelect(this.form.BusDT2)" /><br />
								<input type="button" value="&lt;--"onclick="moveOptions(this.form.BusDT2, this.form.BusDT1); sortSelect(this.form.BusDT1)" />
							</td>
							<td>
								<select name="BusDT2" size="10" multiple="multiple" id="collectBusDTIN">';
		$output .= 					$resIN;
		$output .= '   			</select>
							</td>
						</tr>
					</table>';
		$output .= '<br>';
		if ($display)
			return $output;
		else
			return "";
	}
	function SubwayMainOutput()
	{
		$display = false;
		$output = 'Select the Main World Subways you like to process:';
		if(!isset($this->settings['collectSubwayMainIN'])) $this->settings['collectSubwayMainIN'] = array();
		$resNOT="";
		$resN = array();
		$resIN="";
		$resI = array();
		foreach ($this->SubwayMainObjects as $key=>$item)
		{
			$display = true;
			$found = "N";
			if (in_array((string)$item['id'],$this->settings['collectSubwayMainIN'])) {$found = "Y";}
			if($found == "Y" )
				{ $resI[$item['id']] = (string)$item['itemName'];}
			else
				{ $resN[$item['id']] = (string)$item['itemName'];}
		}
		natcasesort($resI);
		natcasesort($resN);
		foreach ($resI as $id=>$item)
		{
			$resIN .= '<option value="'.$id.'">'.$item." ($id)</option>";
		}
		foreach ($resN as $id=>$item)
		{
			$resNOT .= '<option value="'.$id.'">'.$item." ($id)</option>";
		}
		$output .= '<table class="table0">
						<th>Do NOT collect</th><th></th><th>Do collect</th>
						<tr>
							<td>
								<select name="SubMain1" size="10" multiple="multiple">';
		$output .=					$resNOT;
		$output .= '			</select>
							</td>
							<td align="center" valign="middle">
								<input type="button" value="--&gt;"onclick="moveOptions(this.form.SubMain1, this.form.SubMain2); sortSelect(this.form.SubMain2)" /><br />
								<input type="button" value="&lt;--"onclick="moveOptions(this.form.SubMain2, this.form.SubMain1); sortSelect(this.form.SubMain1)" />
							</td>
							<td>
								<select name="SubMain2" size="10" multiple="multiple" id="collectSubwayMainIN">';
		$output .= 					$resIN;
		$output .= '   			</select>
							</td>
						</tr>
					</table>';
		$output .= '<br>';
		if ($display)
			return $output;
		else
			return "";
	}

	function SubwayDTOutput()
	{
		$display = false;
		$output = 'Select the Downtown Subways you like to process:';
		if(!isset($this->settings['collectSubwayDTIN'])) $this->settings['collectSubwayDTIN'] = array();
		$resNOT="";
		$resN = array();
		$resIN="";
		$resI = array();
		foreach ($this->SubwayDTObjects as $key=>$item)
		{
			$display = true;
			$found = "N";
			if (in_array((string)$item['id'],$this->settings['collectSubwayDTIN'])) {$found = "Y";}
			if($found == "Y" )
				{ $resI[$item['id']] = (string)$item['itemName'];}
			else
				{ $resN[$item['id']] = (string)$item['itemName'];}
		}
		natcasesort($resI);
		natcasesort($resN);
		foreach ($resI as $id=>$item)
		{
			$resIN .= '<option value="'.$id.'">'.$item." ($id)</option>";
		}
		foreach ($resN as $id=>$item)
		{
			$resNOT .= '<option value="'.$id.'">'.$item." ($id)</option>";
		}
		$output .= '<table class="table0">
						<th>Do NOT collect</th><th></th><th>Do collect</th>
						<tr>
							<td>
								<select name="SubDT1" size="10" multiple="multiple">';
		$output .=					$resNOT;
		$output .= '			</select>
							</td>
							<td align="center" valign="middle">
								<input type="button" value="--&gt;"onclick="moveOptions(this.form.SubDT1, this.form.SubDT2); sortSelect(this.form.SubDT2)" /><br />
								<input type="button" value="&lt;--"onclick="moveOptions(this.form.SubDT2, this.form.SubDT1); sortSelect(this.form.SubDT1)" />
							</td>
							<td>
								<select name="SubDT2" size="10" multiple="multiple" id="collectSubwayDTIN">';
		$output .= 					$resIN;
		$output .= '   			</select>
							</td>
						</tr>
					</table>';
		$output .= '<br>';
		if ($display)
			return $output;
		else
			return "";
	}
	
	function SubStopOutput()
	{
		$display = false;
		$output = '	<table class="table66">
						<tr height="12">
							<td><b>Subway Stop</b>:</td>
									<td>';
		if (count($this->SubStopMainObjects))
		{
			$display = true;
			$output .= '				<input type="radio" id="collectMainSubStop" name="collect" value="collectMainSubStop" >Use Main World Subway Stop.<br>';
		}
		if (count($this->SubStopDTObjects))
		{
			$display = true;
			$output .= '				<input type="radio" id="collectDTSubStop" checked name="collect" value="collectDTSubStop" >Use Downtown Subway Stop.';
		}
		$output .= '				</td>
								</tr>
							</table>
							<br>';
		if ($display)
			return $output;
		else
			return "";
	}
	
}
?>